
find_package(OpenCL 3.0 REQUIRED)

message(STATUS "Configuring OpenCL")
include(${CMAKE_SOURCE_DIR}/cmake/find_ResourceCreator.cmake)
include(${CMAKE_SOURCE_DIR}/3rdParty/ResourceCreator.cmake/ResourceCreator.cmake)

add_library(OCLWrapper STATIC
    OCLWrapper.h
    OCLWrapper.cpp

    GPU_interface.cpp)

if(CMAKE_SYSTEM_NAME MATCHES "Linux")
    set_target_properties(OCLWrapper PROPERTIES POSITION_INDEPENDENT_CODE TRUE)
endif()

# convert ColorDiff.cl to a resource file (not windows rc). It is generated by ResourceCreator.cmake (3rd party cmake lib)
add_resource_config_time(ColorManip_cl_rc ColorDiff.cl)

target_include_directories(OCLWrapper PRIVATE ${SlopeCraft_Eigen3_include_dir})
target_include_directories(OCLWrapper PUBLIC ${CMAKE_SOURCE_DIR})
target_include_directories(OCLWrapper INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
target_compile_features(OCLWrapper PUBLIC cxx_std_20)
target_link_libraries(OCLWrapper PUBLIC OpenCL::OpenCL ColorManip_cl_rc)

message(STATUS "OpenCL_INCLUDE_DIR = ${OpenCL_INCLUDE_DIR}")
message(STATUS "OpenCL_INCLUDE_DIRS = ${OpenCL_INCLUDE_DIRS}")

if(NOT EXISTS ${OpenCL_INCLUDE_DIR}/CL/cl.hpp)
    target_compile_definitions(OCLWrapper PRIVATE SLOPECRAFT_NO_CL_HPP)
    message(STATUS "Failed to find cl.hpp. This file doesn't exists in opencl include dir. 
        SlopeCraft assumes that opencl.hpp exists, and will try to include it.")

    # file(GLOB_RECURSE cl_h_file "${OpenCL_INCLUDE_DIR}/*/cl.h")
    # file(GLOB_RECURSE cl_hpp_file "${OpenCL_INCLUDE_DIR}/*/cl.hpp")
    # file(GLOB_RECURSE opencl_hpp_file "${OpenCL_INCLUDE_DIR}/*/opencl.hpp")
    # message(WARNING "CL/cl.hpp will not be found correctly.")
    # message(STATUS "Manually found cl_h_file = ${cl_h_file}")
    # message(STATUS "Manually found cl_hpp_file = ${cl_hpp_file}")
    # message(STATUS "Manually found opencl_hpp_file = ${opencl_hpp_file}")
    file(GLOB_RECURSE all_cl_files "${OpenCL_INCLUDE_DIR}/CL/*")
    message(STATUS "All opencl headers: ${all_cl_files}")
else()
    message(STATUS "Found opencl C++ header: ${OpenCL_INCLUDE_DIR}/CL/cl.hpp")
endif()

target_link_libraries(GPUInterface PUBLIC OCLWrapper)
target_compile_definitions(GPUInterface PUBLIC SLOPECRAFT_HAVE_OPENCL)

if(${LINUX})
    set_target_properties(GPUInterface PROPERTIES POSITION_INDEPENDENT_CODE TRUE)
endif()